package com.it.a01mymap;

import java.util.*;

public class A05_HashMapDemo2 {
    public static void main(String[] args) {
        /*
            某个班级80名学生，现在需要组成秋游活动
            班长提供了四个景点依次是（A，B，C，D）
            每一个学生只能选择一个景点，请统计出最终哪个景点想出的人数最多
         */
        //先让学生进行投票
        //定义四个景点
        String[] arr = {"A", "B", "C", "D"};
        //利用随机数模拟80个同学的投票，并把投票结果存储起来
        //用集合存储
        ArrayList<String> list = new ArrayList<String>();
        Random r = new Random();
        for (int i = 0; i < 80; i++){
            int index = r.nextInt(arr.length);
            list.add(arr[index]);
        }
        System.out.println(list);

        HashMap<String,Integer> map = new HashMap<String,Integer>();
        for (String name : list){
            if (map.containsKey(name)){
                int count = map.get(name);
                count++;
                map.put(name, count);
            }else{
                map.put(name, 1);
            }
        }

        System.out.println(map);

        //求最大值
        int max = 0;
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries){
            int count = entry.getValue();
            if (count > max){
                max = count;
            }
        }

        System.out.println(max);

        //
        for(Map.Entry<String, Integer> entry : entries){
            int count = entry.getValue();
            if(count == max){
                System.out.println(entry.getKey());
            }
        }

    }
}
